package com.tang.qzzp.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tang.qzzp.entity.User;
import com.tang.qzzp.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;

/**
 * @Author 唐海清
 * @Version V1.0
 * @Description ......
 */
public class UserRealm extends AuthorizingRealm {
    @Resource
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return new SimpleAuthorizationInfo();
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", token.getUsername());
        User user = userService.getOne(wrapper);
        if (user == null) {
            return null;
        }
        return new SimpleAuthenticationInfo(user, user.getUserPassword(), "");
    }
}
